package com.ruoyi.project.WebMoudle.wxMessage.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.WebMoudle.WxShop.domain.WxShop;
import com.ruoyi.project.WebMoudle.WxShop.service.IWxShopService;
import com.ruoyi.project.WebMoudle.WxUser.domain.WxUser;
import com.ruoyi.project.WebMoudle.WxUser.service.IWxUserService;
import com.ruoyi.project.WebMoudle.shopMessage.domain.ShopMessage;
import com.ruoyi.project.WebMoudle.shopMessage.service.IShopMessageService;
import com.ruoyi.project.WebMoudle.userMessage.domain.UserMessage;
import com.ruoyi.project.WebMoudle.userMessage.service.IUserMessageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.WebMoudle.wxMessage.domain.WxMessage;
import com.ruoyi.project.WebMoudle.wxMessage.service.IWxMessageService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 消息管理Controller
 * 
 * @author ruoyi
 * @date 2020-09-09
 */
@Controller
@RequestMapping("/wxMessage/wxMessage")
public class WxMessageController extends BaseController
{
    private String prefix = "wxMessage/wxMessage";

    @Autowired
    private IWxMessageService wxMessageService;
    @Autowired
    private IUserMessageService userMessageService;
    @Autowired
    private IWxUserService wxUserService;
    @Autowired
    private IShopMessageService shopMessageService;
    @Autowired
    private IWxShopService shopService;


    @RequiresPermissions("wxMessage:wxMessage:view")
    @GetMapping()
    public String wxMessage()
    {
        return prefix + "/wxMessage";
    }

    @RequiresPermissions("wxMessage:wxMessage:view")
    @GetMapping("/selectUser/{msgId}")
    public String selectUser(@PathVariable("msgId") Long msgId, ModelMap mmap)
    {
        mmap.put("msgId",msgId);
        return prefix + "/selectUser";
    }
    @RequiresPermissions("wxMessage:wxMessage:view")
    @GetMapping("/selectShop/{msgId}")
    public String selectShop(@PathVariable("msgId") Long msgId, ModelMap mmap)
    {
        mmap.put("msgId",msgId);
        return prefix + "/selectShop";
    }
    /**
     * 查询消息管理列表
     */
    @RequiresPermissions("wxMessage:wxMessage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WxMessage wxMessage)
    {
        startPage();
        List<WxMessage> list = wxMessageService.selectWxMessageList(wxMessage);
        return getDataTable(list);
    }

    /**
     * 导出消息管理列表
     */
    @RequiresPermissions("wxMessage:wxMessage:export")
    @Log(title = "消息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WxMessage wxMessage)
    {
        List<WxMessage> list = wxMessageService.selectWxMessageList(wxMessage);
        ExcelUtil<WxMessage> util = new ExcelUtil<WxMessage>(WxMessage.class);
        return util.exportExcel(list, "wxMessage");
    }

    /**
     * 新增消息管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存消息管理
     */
    @RequiresPermissions("wxMessage:wxMessage:add")
    @Log(title = "消息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WxMessage wxMessage)
    {
        return toAjax(wxMessageService.insertWxMessage(wxMessage));
    }

    /**
     * 修改消息管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WxMessage wxMessage = wxMessageService.selectWxMessageById(id);
        mmap.put("wxMessage", wxMessage);
        return prefix + "/edit";
    }

    /**
     * 修改保存消息管理
     */
    @RequiresPermissions("wxMessage:wxMessage:edit")
    @Log(title = "消息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WxMessage wxMessage)
    {
        return toAjax(wxMessageService.updateWxMessage(wxMessage));
    }

    /**
     * 删除消息管理
     */
    @RequiresPermissions("wxMessage:wxMessage:remove")
    @Log(title = "消息管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wxMessageService.deleteWxMessageByIds(ids));
    }


    @Log(title = "发送消息给用户", businessType = BusinessType.DELETE)
    @PostMapping( "/msgToUser")
    @ResponseBody
    public AjaxResult msgToUser(Long  msgId,String wids)
    {
        if (!StringUtils.isNotEmpty(wids)){
            return error("请勾选下发对象");
        }
        String[] ids = Convert.toStrArray(wids);
        WxMessage wxMessage = wxMessageService.selectWxMessageById(msgId);
        List<UserMessage> list = new ArrayList<>();
        for (String wid : ids) {     //创建用户消息
            UserMessage userMessage = new UserMessage();
            userMessage.setWxUserId(Long.parseLong(wid));
            userMessage.setMsgTitle(wxMessage.getMsgTitle());
            userMessage.setMsgContent(wxMessage.getMsgContent());
            list.add(userMessage);
        }
        System.out.println(list);
        return toAjax(userMessageService.addSomeMsg(list));
    }

    @Log(title = "发送消息给全部用户", businessType = BusinessType.DELETE)
    @PostMapping( "/msgToAllUser")
    @ResponseBody
    public AjaxResult msgToAllUser(Long  msgId, WxUser wxUser)
    {
        List<WxUser> userList = wxUserService.selectWxUserList(wxUser);
        WxMessage wxMessage = wxMessageService.selectWxMessageById(msgId);
        ArrayList<UserMessage> list = new ArrayList<>();
        if (userList.size()>0){
            for (WxUser user : userList) {
                UserMessage userMessage = new UserMessage();
                userMessage.setWxUserId(user.getId());
                userMessage.setMsgTitle(wxMessage.getMsgTitle());
                userMessage.setMsgContent(wxMessage.getMsgContent());
                list.add(userMessage);
            }
        }else {
            return error("无符合下放对象");
        }
        return toAjax(userMessageService.addSomeMsg(list));
    }

    @Log(title = "发送消息给商铺", businessType = BusinessType.DELETE)
    @PostMapping( "/msgToShop")
    @ResponseBody
    public AjaxResult msgToShop(Long  msgId,String wids)
    {
        if (!StringUtils.isNotEmpty(wids)){
            return error("请勾选下发对象");
        }
        String[] ids = Convert.toStrArray(wids);
        WxMessage wxMessage = wxMessageService.selectWxMessageById(msgId);
        List<ShopMessage> list = new ArrayList<>();
        for (String wid : ids) {     //创建商铺消息
            ShopMessage shopMessage = new ShopMessage();
            shopMessage.setShopId(Long.parseLong(wid));
            shopMessage.setMsgTitle(wxMessage.getMsgTitle());
            shopMessage.setMsgContent(wxMessage.getMsgContent());
            list.add(shopMessage);
        }
        return toAjax(shopMessageService.addSomeMsg(list));
    }

    @Log(title = "发送消息给全部商铺", businessType = BusinessType.DELETE)
    @PostMapping( "/msgToAllShop")
    @ResponseBody
    public AjaxResult msgToAllShop(Long  msgId, WxShop shop)
    {
        shop.setIsValid("1");
        List<WxShop> userList = shopService.selectWxShopList(shop);
        WxMessage wxMessage = wxMessageService.selectWxMessageById(msgId);
        ArrayList<ShopMessage> list = new ArrayList<>();
        if (userList.size()>0){
            for (WxShop wxShop : userList) {
                ShopMessage shopMessage = new ShopMessage();
                shopMessage.setShopId(wxShop.getId());
                shopMessage.setMsgTitle(wxMessage.getMsgTitle());
                shopMessage.setMsgContent(wxMessage.getMsgContent());
                list.add(shopMessage);
            }
        }else {
            return error("无符合下放对象");
        }
        return toAjax(shopMessageService.addSomeMsg(list));
    }

}
